﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;

namespace EXAM
{
    public partial class Default : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string typeGadget = "";
                if (Request["TypeGadget"] != null)
                    typeGadget = Request["TypeGadget"].ToString();
                BUS.GadgetItemsControl gadCtr = new BUS.GadgetItemsControl();
                DataTable dt = gadCtr.GetParentName(typeGadget, ref sErr);

                lvDB.DataSource = dt;
                lvDB.DataBind();
                DataTable dtNew = gadCtr.GetNew(typeGadget, ref sErr);
                dgvNew.DataSource = dtNew;
                dgvNew.DataBind();
                //DTO.UserInfo uinf = Session["UserGuest"] as DTO.UserInfo;
                //string pathTemp = MapPath("~") + "Template\\";
                //string pathRepr = MapPath("~") + "\\";
                //ReportGenerator rg = new ReportGenerator("DBOARD", "EXA", ConfigurationSettings.AppSettings["strDesConnection"].ToString(), pathTemp, pathRepr);

                //if (uinf != null)
                //{
                //    Object[] arrObj = new Object[] { uinf.ID };
                //    rg.SetParameter(arrObj);
                //}
                //rg.ExportExcelToPath(pathRepr);
                //string filename = rg.ExportHTMLToFile(pathRepr, "DBOARD.html");
                //StreamReader reader = new StreamReader(filename);
                //string content = reader.ReadToEnd();
                //reader.Close();
                //lbContent.Text = content;

            }

        }

        private void LoadGadgets(EXAM.UserControl.ucGarget gad, string parent)
        {
            //BUS.GadgetItemsControl gadCtr = new BUS.GadgetItemsControl();
            //DataTable dt = gadCtr.GetParentName(ref sErr);
            //lvDB.DataSource = dt;
            //lvDB.DataBind();
            //foreach (DataRow row in dt.Rows)
            //{
            string typeGadget = "";
            if (Request["TypeGadget"] != null)
                typeGadget = Request["TypeGadget"].ToString();
            BUS.GadgetItemsControl itemCtr = new BUS.GadgetItemsControl();
            DataTable dtx = itemCtr.GetByGadget(parent, typeGadget, ref sErr);
            DataRow[] arrRows = dtx.Select("IsDefault ='T'");
            if (arrRows.Length > 0)
            {
                DTO.GadgetItemsInfo infGad = new DTO.GadgetItemsInfo(arrRows[0]);
                string defaultlink = String.Format("<a class='active' href='{0}' target='{1}' style='color:yellow'>{2}</a>", "LearnLog.aspx?id=" + infGad.ID, infGad.Target, infGad.Name);
                gad.DefaultLink = defaultlink;
                dtx.Rows.Remove(arrRows[0]);
            }
            //= (EXAM.UserControl.ucGarget)Page.LoadControl("UserControl/ucGarget.ascx");
            gad.Width = "256px";
            gad.Title = parent;
            gad.Datasource = dtx;
            //Panel1.Controls.Add(gad);
            gad.DataBind();
            //}
            //Panel1.DataBind();
        }

        protected void lvDB_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item != null)
            {
                if (e.Item is System.Web.UI.WebControls.DataListItem)
                {
                    DataRowView tmp = (DataRowView)((System.Web.UI.WebControls.DataListItem)e.Item).DataItem;
                    Control x = e.Item.FindControl("ucGarget1");
                    if (x is EXAM.UserControl.ucGarget)
                        LoadGadgets((EXAM.UserControl.ucGarget)x, tmp["Parent"].ToString());
                }
            }
        }


    }
}
